<?php
namespace app\admin\controller\Meta;
use support\exception\ValidateException;
use app\admin\model\Meta\Ue as UeModel;
use app\admin\controller\CommonController;
use think\facade\Db;

class UeController extends CommonController{



	/**
	 *@description 数据列表
	 *@buildcode(true)
	*/
	function index(){
		if($this->request->method() == 'GET'){
			return view('meta/ue/index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$page = $this->request->post('page', 1, 'intval');

			$field = 'ue_id,meta_id,name,ttsType_id,sttType_id,chat_id,role_id,username,password,wakeup_keyword,avatar,basicInformation,openingLine,traits,status,create_time,update_time';
			$query = UeModel::field($field);

			$param = $this->request->post();
			if(isset($param['ue_id']) && !empty($param['ue_id'])) {
				$query = $query->where("ue.ue_id", $param['ue_id']);
			}
			if(isset($param['name']) && $param['name'] != null) {
				$query = $query->whereLike("ue.name", "%".$param['name']."%");
			}
			if(isset($param['model_id']) && $param['model_id'] != null) {
				$query = $query->where("ue.model_id", $param['model_id']);
			}
			if(isset($param['ttsType_id']) && $param['ttsType_id'] != null) {
				$query = $query->where("ue.ttsType_id", $param['ttsType_id']);
			}
			if(isset($param['sttType_id']) && $param['sttType_id'] != null) {
				$query = $query->where("ue.sttType_id", $param['sttType_id']);
			}
			if(isset($param['chat_id']) && $param['chat_id'] != null) {
				$query = $query->where("ue.chat_id", $param['chat_id']);
			}
			if(isset($param['role_id']) && $param['role_id'] != null) {
				$query = $query->where("ue.role_id", $param['role_id']);
			}
			if(isset($param['username']) && $param['username'] != null) {
				$query = $query->whereLike("ue.username", "%".$param['username']."%");
			}
			if(isset($param['wakeup_keyword']) && $param['wakeup_keyword'] != null) {
				$query = $query->whereLike('ue.wakeup_keyword', "%".$param['wakeup_keyword']."%");
			}
			if(isset($param['traits']) && $param['traits'] != null) {
				$query = $query->where("ue.traits", $param['traits']);
			}
			if(isset($param['status']) && $param['status'] != null) {
				$query = $query->where("ue.status", $param['status']);
			}
			$withJoin = [
				'uemodel'=>explode(',','name,sex,img'),
			];
			$query = $query->withJoin($withJoin,'left');

			$orderby = ($param['sort'] && $param['order']) ? $param['sort'].' '.$param['order'] : 'ue_id desc';

			$res =$query->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			$page == 1 && $data['sql_field_data'] = $this->getSqlField('model_id,ttsType_id,sttType_id,chat_id');
			return json($data);
		}
	}


	/**
	 *@description 修改排序开关
	 *@buildcode(true)
	*/
	function updateExt(){
		$postField = 'ue_id,';
		$data = $this->request->only(explode(',',$postField),'post');
		if(!$data['ue_id']) throw new ValidateException ('参数错误');
		UeModel::update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/**
	 *@description 添加
	 *@buildcode(true)
	*/
	public function add(){
		$postField = 'meta_id,name,model_id,ttsType_id,sttType_id,chat_id,role_id,username,password,wakeup_keyword,avatar,basicInformation,openingLine,traits,status,create_time,update_time';
		$data = $this->request->only(explode(',',$postField),'post');

		$validate = new \app\admin\validate\Meta\UeValidate;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['meta_id'] = random(15,'all');
		$data['password'] = md5($data['password'].config('my.password_secrect'));
		$data['wakeup_keyword'] = is_array($data['wakeup_keyword']) ? implode(',',$data['wakeup_keyword']) : '';
		$data['create_time'] = time();

		if($ret = hook('app/admin/hook/Meta/Ue@beforAdd',$data)){
			return $ret;
		}

		try{
			$res = UeModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/**
	 *@description 修改
	 *@buildcode(true)
	*/
	public function update(){
		$postField = 'ue_id,meta_id,name,model_id,ttsType_id,sttType_id,chat_id,role_id,username,wakeup_keyword,avatar,basicInformation,openingLine,traits,status,create_time,update_time';
		$data = $this->request->only(explode(',',$postField),'post');

		$validate = new \app\admin\validate\Meta\UeValidate;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['wakeup_keyword'] = is_array($data['wakeup_keyword']) ? implode(',',$data['wakeup_keyword']) : '';
		$data['create_time'] = !empty($data['create_time']) ? strtotime($data['create_time']) : '';
		$data['update_time'] = time();

		try{
			UeModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/**
	 *@description 修改信息之前查询信息的
	 *@buildcode(true)
	*/
	function getUpdateInfo(){
		$id =  $this->request->post('ue_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'ue_id,meta_id,name,model_id,ttsType_id,sttType_id,chat_id,role_id,username,wakeup_keyword,avatar,basicInformation,openingLine,traits,status,create_time,update_time';
		$res = UeModel::field($field)->find($id);
		$res['wakeup_keyword'] = !empty($res['wakeup_keyword']) ? explode(',',$res['wakeup_keyword']) : '';
		return json(['status'=>200,'data'=>$res]);
	}


	/**
	 *@description 删除
	 *@buildcode(true)
	*/
	function delete(){
		$idx =  $this->request->post('ue_id');
		if(!$idx) throw new ValidateException ('参数错误');

		if($ret = hook('app/admin/hook/Meta/Ue@beforDelete',$idx)){
			return $ret;
		}

		UeModel::destroy(['ue_id'=>explode(',',$idx)],true);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/**
	 *@description 查看详情
	 *@buildcode(true)
	*/
	function detail(){
		$id =  $this->request->post('ue_id', '', 'serach_in');
		if(!$id){
			throw new ValidateException ('参数错误');
		}
		$field = 'ue_id,meta_id,name,ttsType_id,sttType_id,chat_id,role_id,username,password,wakeup_keyword,avatar,basicInformation,openingLine,traits,status,create_time,update_time';
		$res = UeModel::field($field)->findOrEmpty($id);

		if($res->isEmpty()){
			throw new ValidateException ('信息不存在');
		}

		return json(['status'=>200,'data'=>$res]);
	}


	/**
	 *@description 获取定义sql语句的字段信息
	 *@buildcode(true)
	*/
	function getFieldList(){
		return json(['status'=>200,'data'=>$this->getSqlField('model_id,ttsType_id,sttType_id,chat_id')]);
	}


	/**
	 *@description 获取定义sql语句的字段信息
	 *@buildcode(true)
	*/
	private function getSqlField($list){
		$data = [];
		if(in_array('model_id',explode(',',$list))){
			$data['model_ids'] = $this->query("select ue_model_id,name from cd_ue_model",'mysql');
		}
		if(in_array('ttsType_id',explode(',',$list))){
			$data['ttsType_ids'] = $this->query("select typetts_id,name from cd_typetts",'mysql');
		}
		if(in_array('sttType_id',explode(',',$list))){
			$data['sttType_ids'] = $this->query("select typestt_id,name from cd_typestt",'mysql');
		}
		if(in_array('chat_id',explode(',',$list))){
			$data['chat_ids'] = $this->query("select chat_id,name from cd_chat",'mysql');
		}
		return $data;
	}


}